package com.chukun.netty.tcpdecoder;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.nio.charset.Charset;
import java.util.UUID;

public class TcpServerHandler extends SimpleChannelInboundHandler<PersonProtocal> {

    private int count;

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, PersonProtocal msg) throws Exception {
        byte[] content = msg.getContent();
        System.out.println("received from client : "+new String(content, Charset.forName("utf-8"))+" counter : "+(++count));

        String uuId = UUID.randomUUID().toString();
        PersonProtocal personProtocal = new PersonProtocal();
        byte[] responseBytes = uuId.getBytes(Charset.forName("utf-8"));
        personProtocal.setLength(responseBytes.length);
        personProtocal.setContent(responseBytes);
        ctx.writeAndFlush(personProtocal);
    }
}
